package com.zenjava.jfx.performance.animate1;

import javafx.animation.ParallelTransitionBuilder;
import javafx.animation.Transition;
import javafx.animation.TranslateTransitionBuilder;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import javafx.util.Duration;

public class SwipeApp extends Application {

    private StackPane animationPane;
    private Pane pane1;
    private Pane pane2;
    private Button showPane2Button;
    private Button showPane1Button;

    public static void main(String[] args) throws Exception {
        launch(args);
    }

    public void start(Stage stage) throws Exception {

        animationPane = new StackPane();
        pane1 = createPane1();
        pane2 = createPane2();
        animationPane.getChildren().add(pane1);

        BorderPane rootPane = new BorderPane();
        rootPane.setTop(createNavBar());

        showPane1Button.setDisable(true);
        rootPane.setCenter(animationPane);

        Scene scene = new Scene(rootPane, 800, 600);
        stage.setScene(scene);
        stage.show();
    }

    protected Pane createNavBar() {

        HBox pane = new HBox(10);
        pane.setPadding(new Insets(5));
        pane.setStyle("-fx-border-color: black; -fx-background-color: linear-gradient(to top, #2465b1 0%, #3678c5 48%, #4285d5 52%, #5496e5 100%)");

        showPane1Button = new Button("Show Pane 1");
        showPane1Button.setOnAction(new EventHandler<ActionEvent>() {
            public void handle(ActionEvent actionEvent) {
                showPane1();
            }
        });
        pane.getChildren().add(showPane1Button);

        showPane2Button = new Button("Show Pane 2");
        showPane2Button.setOnAction(new EventHandler<ActionEvent>() {
            public void handle(ActionEvent actionEvent) {
                showPane2();
            }
        });
        pane.getChildren().add(showPane2Button);

        return pane;
    }

    protected Pane createPane1() {
        VBox pane = new VBox(10);

        Label title = new Label("Pane 1");
        pane.getChildren().add(title);

        return pane;
    }

    protected Pane createPane2() {
        VBox pane = new VBox(10);

        Label title = new Label("Pane 2");
        pane.getChildren().add(title);

        return pane;
    }

    protected void showPane1() {

        // setup pane1 just off screen, ready to animate in
        pane1.setTranslateX(-pane1.getWidth());
        animationPane.getChildren().add(pane1);

        showPane1Button.setDisable(true);
        showPane2Button.setDisable(true);

        Transition transition = ParallelTransitionBuilder.create()

                .children(

                        // exit pane2
                        TranslateTransitionBuilder.create()
                                .node(pane2)
                                .duration(Duration.millis(500))
                                .toX(pane2.getWidth())
                                .build(),

                        // enter pane1
                        TranslateTransitionBuilder.create()
                                .node(pane1)
                                .duration(Duration.millis(500))
                                .toX(0)
                                .build()

                )

                .onFinished(new EventHandler<ActionEvent>() {
                    public void handle(ActionEvent actionEvent) {
                        animationPane.getChildren().remove(pane2);
                        showPane2Button.setDisable(false);
                    }
                })
                .build();

        transition.play();
    }

    protected void showPane2() {

        // setup pane2 just off screen, ready to animate in
        pane2.setTranslateX(animationPane.getWidth());
        animationPane.getChildren().add(pane2);

        showPane1Button.setDisable(true);
        showPane2Button.setDisable(true);

        Transition transition = ParallelTransitionBuilder.create()

                .children(

                        // exit pane1
                        TranslateTransitionBuilder.create()
                                .node(pane1)
                                .duration(Duration.millis(500))
                                .toX(-pane1.getWidth())
                                .build(),

                        // enter pane2
                        TranslateTransitionBuilder.create()
                                .node(pane2)
                                .duration(Duration.millis(500))
                                .toX(0)
                                .build()

                )

                .onFinished(new EventHandler<ActionEvent>() {
                    public void handle(ActionEvent actionEvent) {
                        animationPane.getChildren().remove(pane1);
                        showPane1Button.setDisable(false);
                    }
                })
                .build();

        transition.play();
    }
}
